﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using NineSeas.Models;
using Orchard.ContentManagement.Records;
using Orchard.Tags.Models;
using System.Xml.Serialization;
using System.Web.Script.Serialization;
using Newtonsoft.Json;

namespace NineSeas.ViewModels
{
    public class BookSummaryViewModel
    {
        public int? Id { get; set; }
        public int AuthorId { get; set; }
        public string Name { get; set; }
        public string NameOnUrl { get; set; }
        public string Introduction { get; set; }
        public string ImageUrl { get; set; }
        public string ImageStoragePath { get; set; }
        public string FileUrl { get; set; }
        public string FileStoragePath { get; set; }
        public BookTypeEnum BookType { get; set; }

        public IEnumerable<string> Tags { get; set; }
        public IEnumerable<int> Categories { get; set; }

        public int TotalLikes { get; set; }
        public int TotalViews { get; set; }
        public int WeekLikes { get; set; }
        public int WeekViews { get; set; }

        public DateTime? CreatedDate { get; set; }
        public DateTime? LastUpdatedDate { get; set; }

        public int ChapterCount { get; set; }
        public int DraftCount { get; set; }

        public VersioningViewModel VersionRecord { get; set; }

        public PublishingViewModel Publishing { get; set; }

        public BookSummaryViewModel()
        {
            BookType = BookTypeEnum.Text;
        }

        public BookSummaryViewModel(BookPart part)
        {
            Id = part.Id;
            AuthorId = part.AuthorId;
            Name = part.Name;
            NameOnUrl = part.NameOnUrl;
            Introduction = part.Introduction;
            ImageUrl = part.ImageUrl;
            ImageStoragePath = part.ImageStoragePath;
            FileUrl = part.FileUrl;
            FileStoragePath = part.FileStoragePath;
            BookType = part.BookType;
            TotalLikes = part.TotalLikes;
            TotalViews = part.TotalViews;
            WeekLikes = part.WeekLikes;
            WeekViews = part.WeekViews;
            CreatedDate = part.CreatedDate;
            LastUpdatedDate = part.LastUpdatedDate;
            VersionRecord = new VersioningViewModel {
                Latest = part.ContentItem.VersionRecord.Latest,
                Number = part.ContentItem.VersionRecord.Number,
                Published = part.ContentItem.VersionRecord.Published
            };

            Tags = part.Tags.CurrentTags.Select(t => t.TagName);
            Categories = part.Categories.Terms.Select(t => t.Id);
        }

    }
}